package com.common;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by 10742 on 2017/7/9.
 */
public class PrimTool extends JdbcDaoSupport {

    public int insert(final String primType, final String postSeq, final String reverseSeq, final int moleculeID){
        final String sql = "insert into Prim(PrimType, PostSeq, ReverSeq, Sample_MeasureNO) values(?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setString(1, primType);
                ps.setString(2, postSeq);
                ps.setString(3, reverseSeq);
                ps.setInt(4, moleculeID);
                return ps;
            }
        }, keyHolder);
        return keyHolder.getKey().intValue();
    }

    public int update(String primType, String postSeq, String reverseSeq, int primID){
        String sql = "update Prim set PrimType=?,PostSeq=?,ReverSeq=? where ID=?";
        return getJdbcTemplate().update(sql, primType, postSeq, reverseSeq, primID);
    }

}
